package me.m1key.springtx;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.m1key.springtx.beans.DogsBean;
import me.m1key.springtx.entities.Dog;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

@WebServlet(name = "mytest", urlPatterns = { "/myurl" }, initParams = {
		@WebInitParam(name = "n1", value = "v1"),
		@WebInitParam(name = "n2", value = "v2") })
public class LauncherServlet extends HttpServlet {

	private static ApplicationContext appContext = new ClassPathXmlApplicationContext(
			new String[] { "applicationContext.xml" });

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse response)
			throws ServletException, IOException {
		doStuff();
		response.setContentType("text/plain");
	}

	private void doStuff() {
		DogsBean dogsBean = appContext.getBean("dogsBean", DogsBean.class);

		Dog doggie = new Dog();
		doggie.setName("Sega");

		System.out.println("Any dogs: " + dogsBean.containsDogs());
		dogsBean.persistDog(doggie);
		System.out.println("Any dogs: " + dogsBean.containsDogs());

		doggie.setName("Segunia");
		dogsBean.updateDog(doggie);

		Dog dog = new Dog();
		dog.setName("Segunia");
		dogsBean.persistDog(dog);
	}

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

}
